package demo.test;
import java.awt.* ;

public class DisplayUnit extends Component {
	public Container parent ;
	
	protected Image image;
	protected Image altImage; // alternative image for event such as click
	
	protected String text ;
	Font font ;
	Color foreColor=null ;
	
	boolean movable=true  ;
	boolean activable=true ;
	boolean activated=false ;
	
	protected int x,y ;
	protected int textX=0, textY=0 ; // the position for drawing text
			
	protected int width,height ; // the display width and height 

	
	public DisplayUnit() {
		super() ;
	}
	
	public DisplayUnit(String text, 
					int _x, int _y, int _w, int _h) {
		this(null,text,_x,_y,_w,_h) ;
	}

	public DisplayUnit(Image img, 
					int _x, int _y, int _w, int _h) {
		this(img,null,_x,_y,_w,_h) ;				
	}

	public DisplayUnit(Image img, String _text,
					int _x, int _y, int _w, int _h) {
		super() ;
		image = img ;
		altImage = img ;
		text = _text ;
		x = _x ;
		y = _y ;
		width = _w ;
		height = _h ;
		super.setBounds(x,y,width,height) ;
	}
	
	public void setImage(Image img) {
		this.image = img ;
		setAltImage(img) ;
	}
	
	public void setAltImage(Image _alt) {
		altImage = _alt ;
	}
	
	public void setText(String _text) {
		this.text = _text ;
	}
	public String getText() {
		return text ;
	}
	public void setFont(Font f) {
		font = f ;
	}
	public Font getFont() {
		return font ;
	}
	public void setForeground(Color c ) {
		//super.setForeground(c) ;
		foreColor = c ;
	}
	public void setParent(Container _p) {
		parent = _p ;
	}
	public void setWidth(int width) {
		this.width = width ;
		super.setSize(width,height) ;
	}
	public int getWidth() {
		return width ;
	}
	public void setHeight(int height) {
		this.height = height ;
		super.setSize(width,height) ;		
	}
	public int getHeight() {
		return height ;
	}
	public void setSize(int w, int h) {
		width = w ; height = h ;
		super.setSize(width,height) ;
	}

	public void setPosition( int _x , int _y) {
		x = _x ; y = _y ;
		super.setBounds(x,y,width,height) ;
	}
	
	public void setTextRelativePostion( int _x, int _y) {
		textX = _x ; textY = _y ;
	}
	
	public int getX(){
		return x ;
	}
	public int getY() {
		return y ;
	}
	
	public void setMovable(boolean m) {
		movable = m ;
	}
	public boolean isMovable() {
		return movable ;
	}
	
	public void setActivable(boolean a) {
		activable = a ;
	}
	public boolean isActivable() {
		return activable ;
	}
	
	public void setActivated(boolean a) {
		if(isActivable())
			activated = a ;
		else 
			activated = false ;
	}
	public boolean isActivated() {
		return activated ;
	}
	
	public void paint(Graphics g) {
		if (image!=null) {
			if (isActivated()){
				if ( altImage==image) {
				g.drawImage(altImage, x+width*5/100,y+height*5/100,
						width*9/10,height*9/10,parent) ;
				} else {
				g.drawImage(altImage, x,y,width,height,this) ;
				}
			} else 
				g.drawImage(image,x,y,width,height,this) ;
		}
		
		if ( text!=null) {
			Font f = g.getFont() ;
			if (font!=null) {
				g.setFont(font) ;	
			}

			Color c = g.getColor() ;
			if ( foreColor!=null) {
				g.setColor(foreColor) ;			
			}
			Shape s= g.getClip() ;
			g.setClip(x,y,width,height) ;
			
			FontMetrics fm = g.getFontMetrics() ;
			int ascent = fm.getAscent() ;
			g.drawString(text,x+textX,y+textY+ascent) ;
//			if (isActivated()) {
//				Color cc = g.getColor() ;
//				g.setColor(new Color( 255-cc.getRed(), 255-cc.getGreen(), 255-cc.getBlue() ) );
//				g.drawString(text, x+textX, y+textY+ascent) ;
//			}
			g.setClip(s) ;
			g.setFont(f) ;
			g.setColor(c) ;
		}
		
	}

	public boolean inSelectArea(int _x, int _y){
		if (!isActivable() ) 
			return false ;
		if ( (_x>=x)&&( _x< x+width ) && (_y>=y) && (_y<y+height) )
			return true ;
		else 
			return false ;
	}
	
}